﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.Visible = false;
        FormView1.Visible = false;
    }
    //protected void btnIdQuery_Click(object sender, EventArgs e)
    //{

    //}
    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        GridView1.Visible = true;
        GridView1.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //同步DropDownList選項資料到TextBox1上
        GridViewRow gvr;
        TextBox TB;
        DropDownList DropItem = (DropDownList)sender;
        if (Session["editIndex"] != null)
        {
            gvr = GridView1.Rows[(int)Session["editIndex"]];
            TB = (TextBox)gvr.FindControl("TextBox1");
            if (TB != null)
                TB.Text = DropItem.SelectedValue;

        }
        GridView1.Visible = true;
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox tb;
        DropDownList dropDownList = (DropDownList)sender;
        tb = (TextBox)FormView1.Row.FindControl("FISC_BankTypeTextBox");
        if (tb != null)
            tb.Text = dropDownList.SelectedValue;
        FormView1.Visible = true;
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridView1.Visible = true;
        if (Session["editIndex"] == null)
            Session.Add("editIndex", e.NewEditIndex);
        else
            Session["editIndex"] = e.NewEditIndex;
    }
    protected void queryData_Click(object sender, EventArgs e)
    {
        GridView1.Visible = true;
    }
    protected void insertData_Click(object sender, EventArgs e)
    {
        FormView1.DefaultMode = FormViewMode.Insert;
        GridView1.Visible = false;
        FormView1.Visible = true;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridView1.Visible = true;
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.Visible = true;
    }
    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        GridView1.Visible = true;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // update droplist 
        string tbval = null;
        TextBox TB;
        DropDownList DropItem;
        GridViewRow gvr = e.Row;
        TB = (TextBox)gvr.FindControl("TextBox1");
        if (TB != null)
        {
            tbval = TB.Text;
            // Find TropDownList
            DropItem = (DropDownList)gvr.FindControl("DropDownList1");
            DropItem.SelectedIndex = GetSelectIndex(DropItem, tbval);
        }
    }

    private int GetSelectIndex(DropDownList objItem, string strVal)
    {
        int Selitem = 0;
        for (int i = 0; i < objItem.Items.Count; i++)
        {
            if (objItem.Items[i].Value.Equals(strVal))
                Selitem = i;

        }
        return Selitem;
    }
    protected void FISC_BankTypeTextBox_Init(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)sender;
        tb.Text = "1";
    }
    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        GridView1.DataBind();
        GridView1.Visible = true;
    }
}
